<?php
function getIconv($get_data, $to) {
  $work_unit = 50;
  $init_size = strlen($get_data);
  $result_data = "";
  $count = 0;
  for ( $i = 0 ; $i < $init_size ; $i++ ) {
    $cur_char = substr($get_data,$i,1);

    $t = ord($cur_char);
    if ( $t == 9 || $t == 10 || (32 <= $t && $t <= 126) ) {
      $tn = 1;
    }
    else if ( 194 <= $t && $t <= 223 ) {
      $tn = 2;
    }
    else if ( 224 <= $t && $t < 239 ) {
      $tn = 3;
    }
    else if ( 240 <= $t && $t <= 247 ) {
      $tn = 4;
    }
    else if ( 248 <= $t && $t <= 251 ) {
      $tn = 5;
    }
    else if ( $t == 252 || $t == 253 ) {
      $tn = 6;
    }
    else {
      $tn = 1;
    }

    if ( $work_unit < $tn ) {
      break;
    }

    if ( $count + $tn > $work_unit ) {
		if(function_exists('iconv')){
			$temp_data = iconv("utf-8", $to, $work_string);
		}else if(function_exists('mb_convert_encoding')){
			$temp_data = mb_convert_encoding("utf-8", $to, $work_string);
		}else{
			include_once (ROOT.'/iconv.php');
			if(function_exists('iconv')){
				$temp_data = iconv("utf-8", $to, $work_string);
			}
		}
      $result_data .= $temp_data;

      $work_string = "";
      $i--;
      $count = 0;
    }
    else {
      for ( $j = 0 ; $j < $tn ; $j++ ) {
        $work_string .= $cur_char;
        $i++;
        $count++;
        $cur_char = substr($get_data,$i,1);
      }
      $i--;
    }
  }

  if ( $work_string ) {
		if(function_exists('iconv')){
			$temp_data = iconv("utf-8", $to, $work_string);
		}else if(function_exists('mb_convert_encoding')){
			$temp_data = mb_convert_encoding("utf-8", $to, $work_string);
		}else{
			include_once (ROOT.'/iconv.php');
			if(function_exists('iconv')){
				$temp_data = iconv("utf-8", $to, $work_string);
			}
		}
    $result_data .= $temp_data;
  }

  return $result_data;
} 
?>
